//  
//  Copyright (C) 2009 GNOME Do
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later verion.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;

using Cairo;
using Gdk;
using Gtk;
using GLib;
using NDesk.DBus;

using Do.Platform;
using Do.Interface;

using Docky.Core;
using Docky.Interface;
using Docky.Interface.Menus;

namespace ThunderbirdCount {
		
	public class ThunderbirdCount : AbstractDockletItem, IRightClickable {
		
		const string BottomSvg  = "Thunderbird.svg";
		const string InsideSvg  = "green_dot.svg";
		//const string TopSvg     = "battery_top.svg";
		
		uint timer;
		
		public override string Name {
			get {
				return "Thunderbird Mail Count";
			}
		}
		
		public ThunderbirdCount() {
			UpdateMailCount();
			
			timer = GLib.Timeout.Add (5 * 1000, UpdateMailCount );
		}
		
		public override void Enable() {
		}
		
		int GetEmailCount() {
			string TBCountPath = Environment.GetFolderPath( Environment.SpecialFolder.Personal ) + "/.thunderbird_docky";
			int TBCount = 0;
			
			if( File.Exists( TBCountPath ) ) {
				using ( StreamReader reader = new StreamReader( TBCountPath ) ) {
					if( !reader.EndOfStream ) {
						string tmp = reader.ReadLine();
						if( tmp == "-" )
							TBCount = -1;
						else
							TBCount = Convert.ToInt32( tmp );
					}
				}
			}
			SetText( string.Format( "{0} New Messages", TBCount ) );
			return TBCount;
		}
		
		bool UpdateMailCount() {
			RedrawIcon();
			return true;
		}
		
		void RenderSvgOnContext (Context cr, string resource, int size, int x, int y) {
			Gdk.Pixbuf pbuf = Do.Interface.IconProvider.PixbufFromIconName (resource, size);
			if (pbuf == null)
				return;
			
			CairoHelper.SetSourcePixbuf (cr, pbuf, x + ((pbuf.Width - size) / 2), y + ((pbuf.Height - size) / 2));
			cr.Paint ();
		}
		
		protected override Surface MakeIconSurface (Cairo.Surface similar, int size) {
			Surface sr = similar.CreateSimilar (similar.Content, size, size);
			using (Context cr = new Context (sr)) {
				RenderSvgOnContext (cr, BottomSvg + "@" + GetType ().Assembly.FullName, size, 0, 0);
				
				int thecount = GetEmailCount();
				
				if ( thecount > 0) {
					RenderSvgOnContext (cr, InsideSvg + "@" + GetType ().Assembly.FullName, size, 0, 0);
				
				//new text stuff.		
					TextRenderContext textContext = new TextRenderContext (cr, string.Empty, size);
					textContext.Alignment = Pango.Alignment.Center;
					textContext.EllipsizeMode = Pango.EllipsizeMode.None;
						
					textContext.Text = string.Format ( "<b>{0}</b>", thecount );
					textContext.FontSize = (int) (size / 5);
					textContext.LeftCenteredPoint = new Gdk.Point (-((size/16)*4), ((size - textContext.FontSize)/8)*8);
					DockServices.DrawingService.TextPathAtPoint (textContext);
					cr.LineWidth = 1;
					
					cr.Color = new Cairo.Color (255, 255, 255, 0.8);
					
					cr.Fill ();
				}
			}
			
			return sr;
		}
		
		public override void Dispose () {
			GLib.Source.Remove( timer );
			base.Dispose ();
		}
		
		public void donothing() {
			SetText( "right clicked" );
		}
		
		/**
		 * This is the action taken when the "Compose New Message" menu item has been clicked
		 */
		public void Compose() {
			try{
				Do.Platform.Services.Environment.Execute( "thunderbird -compose" );
			}catch {}
		}
		
		/**
		 * This is the action taken when the "Open Thunderbird" menu item has been clicked
		 */
		public void openThunderbird() {
			try{
				Do.Platform.Services.Environment.Execute( "thunderbird" );
			}catch{ }
		}
		
		public void closeThunderbird() {
			try{
				
			}catch{ }
		}
		
		public override void Clicked( uint button, Gdk.ModifierType state, PointD position ) {
			if( button == 1 ) { //left click
				AnimationType = ClickAnimationType.Bounce;
				openThunderbird();
			} else {
				//basically do nothing.  Need to figure out how to lauch the context menu.
				AnimationType = ClickAnimationType.None;
			}
			base.Clicked( button, state, position );
		}
		
		#region IRightClickable implementation 
		
		public event EventHandler RemoveClicked;
		
		/**
		 * In order for this to work the class must extend IRightClickable
		 */
		public IEnumerable<AbstractMenuArgs> GetMenuItems () {
				
			yield return new SimpleMenuButtonArgs( Compose, "Compose New Message", Gtk.Stock.Refresh );
			
			yield return new SeparatorMenuButtonArgs ();
			
			if( GetEmailCount() < 0 ) 
				yield return new SimpleMenuButtonArgs( openThunderbird, "Open Thunderbird", Gtk.Stock.Open );
			else
				yield return new SimpleMenuButtonArgs( closeThunderbird, "Close Thunderbird", Gtk.Stock.Close );
		}
		
		#endregion 
	}
}
